package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.pojo.DtsAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class AdminServiceImpl implements AdminService{
    @Autowired
    private DtsAdminMapper adminMapper;


    @Override
    public List<DtsAdmin> findAdmin(String username) {
        QueryWrapper<DtsAdmin> qw = new QueryWrapper<>();
        qw.eq("username",username);
        qw.eq("deleted",false);

        List<DtsAdmin> adminList = adminMapper.selectList(qw);

        return adminList;
    }

    @Override
    public IPage<DtsAdmin> adminListPage(Integer page, Integer limit, String sort, String order, String username) {
        IPage<DtsAdmin> iPage = new Page<>(page,limit);

        QueryWrapper<DtsAdmin> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(username)) {
            qw.like("username",username);
        }
        qw.eq("deleted",false);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if ("asc".equalsIgnoreCase(order)) {
                qw.orderByAsc(sort);
            } else {
                qw.orderByDesc(sort);
            }
        }

        iPage = adminMapper.selectPage(iPage,qw);

        return iPage;
    }

    @Override
    public void insertAdmin(DtsAdmin admin) {
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        admin.setDeleted(false);

        adminMapper.insert(admin);
    }

    @Override
    public List<DtsAdmin> findOthersById(Integer id, String username) {
        QueryWrapper<DtsAdmin> qw = new QueryWrapper<>();
        if (username != null) {
            qw.eq("username",username);
        }
        qw.ne("id",id);
        qw.eq("deleted",false);

        List<DtsAdmin> adminList = adminMapper.selectList(qw);

        return adminList;
    }

    @Override
    public DtsAdmin findAdminById(Integer id) {
        DtsAdmin admin = adminMapper.selectById(id);

        return admin;
    }

    @Override
    public void update(DtsAdmin admin) {
        admin.setUpdateTime(new Date());

        adminMapper.updateById(admin);
    }

    @Override
    public void delete(DtsAdmin admin) {
        admin.setDeleted(true);

        adminMapper.updateById(admin);
    }

    @Override
    public List<Map<String, Object>> adminList() {
        QueryWrapper<DtsAdmin> qw = new QueryWrapper<>();
        qw.eq("deleted",false);

        List<DtsAdmin> admins = adminMapper.selectList(qw);

        List<Map<String,Object>> adminList = new ArrayList<>();
        for (DtsAdmin admin : admins) {
            Map<String,Object> map = new HashMap<>();
            map.put("value",admin.getId());
            map.put("label",admin.getUsername());
            adminList.add(map);
        }


        return adminList;
    }


    @Override
    public List<DtsAdmin> findAdminByUserName(String userName) {
        //创建查询条件对象
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", userName);
        queryWrapper.eq("deleted", false); //false对应0,  true对应1

        //查询并返回结果
        List<DtsAdmin> list = adminMapper.selectList(queryWrapper);
        return list;
    }
}
